package dao;

import domain.Admin;
import domain.Course;
import domain.Student;
import util.DBUtil;
import util.Pager;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class LoginOrRegisterDAOImpl {
    public Student getStuInfo(String username) {
        String sql = "select * from student where name = ?";
        try {
            Object[] params={username};
            // 创建QueryRunner对象
            ResultSet rs = DBUtil.executeQuery(sql, params);
            // 执行查询， 返回结果的第一行的第一列
            Student student = new Student();
            if(rs.next()){
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setPassword(rs.getString("password"));
            }
            if (student.getName() == null) {
                return null;
            }
            return student;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    public void register(String username, String password) {
        String sql = "INSERT INTO student(name, password) VALUES (?,?)";
        Object[] params={username, password};
        DBUtil.executeUpdate(sql, params);


    }
    //获取用户登录的账号密码
    public Admin getUserInfo(String username) {
        String sql = "select * from admin where username = ?";
        try {
            Object[] params={username};
            // 创建QueryRunner对象
            ResultSet rs = DBUtil.executeQuery(sql, params);
            // 执行查询， 返回结果的第一行的第一列
            Admin admin = new Admin();
            if(rs.next()){
                admin.setId(rs.getInt("id"));
                admin.setName(rs.getString("name"));
                admin.setPassword(rs.getString("password"));
                admin.setUsername(rs.getString("username"));
            }
            if (admin.getUsername() == null) {
                return null;
            }
            return admin;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

}
